<?php
/**
 * Widget Model
 *
 * @package  Widgets
 * @author   Wing <wxywing@gmail.com>
 * @version  $Id: WidgetModel.class.php Thu Apr 19 06:26:20 GMT 2012 wing $ 
 */
class WidgetModel extends Model
{
	protected $tableName = 'widgets';
	private $valid = array();
	private $fileWidgets = array();
	protected $fields =	array (0 => 'id', 1 => 'widget_name', 2 => 'widget_alias', 3 => 'params', 4 => 'role_id', 5 => 'widget_type', 6 => 'description', 7 => 'status', '_autoinc' => true, '_pk' => 'id');

	public function getWidgetList()
	{
        $result = $this->findAll();
		return $result;
	}
 
	public function getWidgetInfo($id)
	{
		$map['id'] = intval($id);
		return $this->where($map)->find();
	}
	
	public function getWidgetInfoByName($name)
	{
		$map['widget_name'] = $name;
		return $this->where($map)->find();
	}

	private function _getFileWidgets()
	{
		if(!empty($this->fileWidgets)) return $this->fileWidgets;
		//获取文件夹下面的所有Widget
		$dirName = ADDON_PATH . '/widgets/';
		$dir = dir($dirName);
		$fileWidgets = array();
		while (false !== $entry = $dir->read()) {
			if ($entry == '.' || $entry == '..' || $entry==".svn" || strrpos($entry,'.html')) {
				continue;
			}
			$entry = str_replace('.class.php', '', $entry);

			$WidgetsFile = $dirName . DIRECTORY_SEPARATOR . $entry . '.class.php';
			if (is_file($WidgetsFile)) {
				require_once $WidgetsFile;
				$class = $entry ;
				$fileWidgets[$entry] = new $class();
			}
		}
		$this->fileWidgets = $fileWidgets;
	}
	
	public function getWidgetId($appName, $type)
	{
		$widget = $this->where(array('widget_name ' => $appName, 'widget_type' => $type))->find();
		if (empty($widget)) {
			return 0;
		} else {
			return $widget['id'];
		}
	}	
	
	public function getWidgetIdList()
	{
		$result = array();
		
		$list = $this->findAll();
		foreach ($list as $key => $value)
		{
			$result[] = $value['id'];
		}
		return $result;
	}
}
